///////////////////////////////////////
//    Autor: Mato Kormuth              //
//    (C) Mato Programs 2011        //
//////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Mod_Manager
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// Fade: 1 = FadeIn, 2 = FadeOut
        /// </summary>
        public int fade = 1;

        public int close = 0;

        public Form1()
        {
            InitializeComponent();
            FadeIn();
            LoadModsToListbox();
        }

        public void FadeIn()
        {
            fade = 1;
            timer1.Start();
        }

        public void FadeOut()
        {
            fade = 2;
            timer1.Start();
        }

        public void LoadModsToListbox()
        {
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Application.StartupPath + "/data/modmanager/mods/");
            foreach (System.IO.DirectoryInfo dir in di.GetDirectories())
            {
                Console.WriteLine("{0}", dir.Name);
                listBox1.Items.Add(dir.Name);
            }
        }

        public void UpdateModInformation()
        {
            label4.Text = listBox1.SelectedItem.ToString();
            RichTextBox rtb = new RichTextBox();
            try
            {
                rtb.LoadFile(Application.StartupPath + "/data/modmanager/mods/" + listBox1.SelectedItem.ToString() + "/pathtoapplication.txt", RichTextBoxStreamType.PlainText);
label5.Text = rtb.Text;
            }
            catch
            {
                label5.Text = "Error loading application path please review mod properties";
            }
            
        }

        public string ReturnWorkingDir(string pathtomarbleblast)
        {
            if(pathtomarbleblast.Contains("marbleblast.exe"))
            {
                return pathtomarbleblast.Replace("marbleblast.exe", "");
            }
            if (pathtomarbleblast.Contains("marbleblastgold.exe"))
            {
                return pathtomarbleblast.Replace("marbleblastgold.exe", "");
            }
            else
            {
                return "error";
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Change label4 - name and label5 - path to application
            UpdateModInformation();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (fade == 1)
            {
                if (this.Opacity < 1)
                {
                    this.Opacity += 0.01;
                }
                else
                {
                    timer1.Stop();
                }
            }
            else
            {
                if (this.Opacity > 0)
                {
                    this.Opacity -= 0.01;
                }
                else
                {
                    timer1.Stop();
                    close = 1;
                    this.Close();
                }
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (close != 1)
            {
                e.Cancel = true;
                FadeOut();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = label5.Text;
            p.StartInfo.WorkingDirectory = ReturnWorkingDir(label5.Text);
            p.Exited += new EventHandler(p_Exited);
            p.Start();
            button1.Enabled = false;
            button1.Text = "Game running...";
        }

        void p_Exited(object sender, EventArgs e)
        {
            button1.Enabled = true;
            button1.Text = "Play";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Edit with MBMC
            RichTextBox rtbsave = new RichTextBox();
            rtbsave.Text = label5.Text;
            rtbsave.SaveFile(Application.StartupPath + "/mbmmout.txt", RichTextBoxStreamType.PlainText);
            System.Diagnostics.Process prcss = new System.Diagnostics.Process();
            prcss.StartInfo.FileName = Application.StartupPath + "/MBMC.exe";
            prcss.StartInfo.Arguments = "-frommbmm";
            prcss.Start();
            Application.Exit();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Do you sure remove selected mod?", "Marble Blast Mods Manager", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                System.IO.Directory.Delete(Application.StartupPath + "/data/modmanager/mods/" + listBox1.SelectedItem.ToString(),true);
            }
        }
    }
}